/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.metadata.schemasutilities;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import uk.ljmu.metadata.utilities.MetadataUtilities;

public class DcUtilities {

	private static final Logger log = Logger.getLogger(DcUtilities.class);
	
	/**
	 * Constructs and Empty OAI_DC xml record
	 *  
	 * @return new empty OAI_DC document
	 */
	public static Document constructDCEmptyDocument()
	{
		//Create document
		Element root = DocumentHelper.createElement( "oai_dc:dc" );
		root.addNamespace("oai_dc", MetadataUtilities.RELS_MAP.get("oai_dc"));
		root.addNamespace("dc", MetadataUtilities.RELS_MAP.get("dc"));
		Document doc = DocumentHelper.createDocument(root);

		return doc;
	}

	/**
	 * Add a property to an existing OAI_DC record
	 * 
	 * @param propertyName string containing the name of the property
	 * @param propertyValue value of the property to add
	 * @param xmlContents string containing the actual XML document in which to add the new property element
	 * 
	 * @return Document with the properties added
	 */
	public static boolean addOAIDCproperty(String propertyName, String propertyValue, Document xmlDoc)
	{
		if (xmlDoc != null)
		{
			Element root = xmlDoc.getRootElement();
			Element property = DocumentHelper.createElement(propertyName);
			property.setText(propertyValue);
			root.add(property);

			return true;
		} 
		else {
			log.fatal("Error while creating XML Document in: " + DcUtilities.class.getName());
			return false;
		}
	}
}
